package com.baiytfp.hf.HFDataExchange.action.controller.service;

import com.baiytfp.hf.baiytfp.common.db.DBManager;
import com.baiytfp.hf.baiytfp.common.db.HFPager;
import com.baiytfp.hf.baiytfp.framework.base.BaseController;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.baiytfp.hfutils.HFResponse;
import com.baiytfp.hf.baiytfp.hfutils.HFWeb;
import com.baiytfp.hf.HFDataExchange.action.service.PositionService;
import com.baiytfp.hf.HFDataExchange.action.service.RoleService;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.Position;
import com.baiytfp.hf.HFDataExchange.common.model.table.Position_type;
import com.baiytfp.hf.HFDataExchange.common.model.table.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/service/position")
public class PositionSController extends BaseController{

	@Autowired
	public PositionService positionService;

	public PositionSController() {

	}

	@RequestMapping(value="/posilist", method= RequestMethod.POST)
	public ModelAndView posilist(HttpServletRequest request,HttpServletResponse response) throws Exception {
		HFPager hfPager = new HFPager(request);

		Map<String, String> map = new HashMap<String, String>();
		map.put("posi_name", HFRequest.getIstance().getString(request, "posi_name", ""));

		hfPager.setParams(map);

		List<Map> list = positionService.getPosiList(hfPager);

		HFResponse.getIstance().returnResult(response, hfPager.getResult());

		return null;
	}

	//编辑
	@RequestMapping(value="/posiedit", method= RequestMethod.POST)
	public ModelAndView posiedit(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String key = HFRequest.getIstance().getString(request, "itemid");

		if("".equals(key) || "0".equals(key)){	//新增
			Position itemObj = HFWeb.getIstance().toBean(request, Position.class);
			itemObj.setId(HFDBHelper.getIDGenerator());
			long res = HFDBHelper.add(TableName.POSITION, itemObj);

			HFResponse.getIstance().returnResultByRes(response, res);
		}else{	//编辑
			Position itemObj = HFDBHelper.read(TableName.POSITION, " where id=? ", Position.class, key);
			itemObj = HFWeb.getIstance().toBean(request, itemObj);

			long res = HFDBHelper.update(TableName.POSITION, " where id = ? ", itemObj, new Object[] { itemObj.getId() });
			HFResponse.getIstance().returnResultByRes(response, res);
		}

		return null;
	}

	//删除 (支持多个ID, 如 1, 2, 3 这种写法)
	@RequestMapping("/posidel")
	public ModelAndView posidel(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String ids = HFRequest.getIstance().getString(request, "itemid");

		Object[] params =  DBManager.getInStrList(ids);

		long rownum = HFDBHelper.rownum("", " select * from " + TableName.USER_POSITION + " t where posi_id in (" + DBManager.getInCharList( params.length) + ") ", params);

		if(rownum > 0){
			HFResponse.getIstance().returnResult(response, -1, "请先解除所有用户的相关岗位信息后，再删除岗位信息，谢谢！", null);
		}else {
			HFResponse.getIstance().returnResultByRes(response, HFDBHelper.delete(TableName.POSITION, " where id in (" + DBManager.getInCharList(params.length) + ") ", params));
		}

		return null;
	}


	@RequestMapping(value="/typelist", method= RequestMethod.POST)
	public ModelAndView typelist(HttpServletRequest request,HttpServletResponse response) throws Exception {
		HFPager hfPager = new HFPager(request);

		Map<String, String> map = new HashMap<String, String>();
		map.put("posi_type_name", HFRequest.getIstance().getString(request, "posi_type_name", ""));

		hfPager.setParams(map);

		List<Position_type> list = positionService.getTypeList(hfPager);


		HFResponse.getIstance().returnResult(response, hfPager.getResult());

		return null;
	}

	//编辑
	@RequestMapping(value="/typeedit", method= RequestMethod.POST)
	public ModelAndView typeedit(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String key = HFRequest.getIstance().getString(request, "itemid");

		if("".equals(key) || "0".equals(key)){	//新增
			Position_type itemObj = HFWeb.getIstance().toBean(request, Position_type.class);
			itemObj.setId(HFDBHelper.getIDGenerator());

			long res = HFDBHelper.add(TableName.POSITION_TYPE, itemObj);

			HFResponse.getIstance().returnResultByRes(response, res);
		}else{	//编辑
			Position_type itemObj = HFDBHelper.read(TableName.POSITION_TYPE, " where id=? ", Position_type.class, key);
			itemObj = HFWeb.getIstance().toBean(request, itemObj);

			long res = HFDBHelper.update(TableName.POSITION_TYPE, " where id = ? ", itemObj, new Object[] { itemObj.getId() });
			HFResponse.getIstance().returnResultByRes(response, res);
		}

		return null;
	}

	//删除 (支持多个ID, 如 1, 2, 3 这种写法)
	@RequestMapping("/typedel")
	public ModelAndView typedel(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String ids = HFRequest.getIstance().getString(request, "itemid");

		Object[] params =  DBManager.getInStrList(ids);

		long rownum = HFDBHelper.rownum("", " select * from " + TableName.POSITION + " t where posi_type in (" + DBManager.getInCharList( params.length) + ") ", params);

		if(rownum > 0){
			HFResponse.getIstance().returnResult(response, -1, "请先删除所有相关岗位信息后，再删除岗位类别，谢谢！", null);
		}else {
			HFResponse.getIstance().returnResultByRes(response, HFDBHelper.delete(TableName.POSITION_TYPE, " where id in (" + DBManager.getInCharList(params.length) + ") ", params));
		}

		return null;
	}
}
